<?php

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace app\admin\controller\shareroom;


use app\common\controller\Adminbase;
use app\admin\model\RoomModel;
/**
 * Description of ShareRoom
 *
 * @author PC
 */
class Room extends Adminbase{
    
//初始化
    protected function initialize()
    {
        parent::initialize();
        $this->modelClass = new RoomModel;

//        $this->childrenAdminIds = $this->auth->getChildrenAdminIds(true);
//        $this->childrenGroupIds = $this->auth->getChildrenGroupIds(true);
//
//        $groupList = AuthGroupModel::where('id', 'in', $this->childrenGroupIds)->select()->toArray();
//        Tree::instance()->init($groupList);
//        $groupdata = [];
//        if ($this->auth->isAdministrator()) {
//            $result = Tree::instance()->getTreeList(Tree::instance()->getTreeArray(0), 'title');
//            foreach ($result as $k => $v) {
//                $groupdata[$v['id']] = $v['title'];
//            }
//        } else {
//            $result = [];
//            $groups = $this->auth->getGroups();
//            foreach ($groups as $m => $n) {
//                $childlist = Tree::instance()->getTreeList(Tree::instance()->getTreeArray($n['id']), 'title');
//                //$temp = [];
//                foreach ($childlist as $k => $v) {
//                    $groupdata[$v['id']] = $v['title'];
//                }
//                //$result[$n['title']] = $temp;
//            }
//            //$groupdata = $result;
//        }
//        $this->assign('groupdata', $groupdata);
    }
    
    public function index(){
        $this->auth->id;
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($page, $limit, $where, $sort, $order) = $this->buildTableParames();

//            $childrenGroupIds = $this->childrenGroupIds;
//            $groupName        = AuthGroupModel::where('id', 'in', $childrenGroupIds)
//                ->column('id,title');

            $list = $this->modelClass
                ->where($where)
//                ->where('id', 'in', $this->childrenAdminIds)
//                ->field(['password', 'salt', 'token'], true)
                ->order($sort, $order)
                ->paginate($limit);

//            foreach ($list as $k => &$v) {
//                $v['groups']      = $groupName[$v['roleid']] ?? '未知';
//            }
//            unset($v);
            $result = ["code" => 0, 'count' => $list->total(), "data" => $list->items()];
            return json($result);
        }
        return $this->fetch();
    }
    
    /**
     * 添加房间
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
            $params             = $this->request->post('');
            $result             = $this->validate($params, 'RoomValidate.insert');
            if (true !== $result) {
                return $this->error($result);
            }
           
            try {
                $this->modelClass->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("添加成功！", url('index'));
        }
        return $this->fetch();
    }
    
    /**
     * 房间编辑
     */
    public function edit()
    {
        $id  = $this->request->param('id/d', 0);
        
        $row = $this->modelClass->get($id);
        if (!$row) {
            $this->error('记录未找到');
        }
        
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post('');
            $result = $this->validate($params, 'RoomValidate.update');
            if (true !== $result) {
                return $this->error($result);
            }
//            if (!in_array($params['roleid'], $this->childrenGroupIds)) {
//                $this->error('没有权限操作！');
//            }
//            //密码为空，表示不修改密码
//            if (isset($params['password']) && $params['password']) {
//                $passwordinfo       = encrypt_password($params['password']); //对密码进行处理
//                $params['encrypt']  = $passwordinfo['encrypt'];
//                $params['password'] = $passwordinfo['password'];
//
//            } else {
//                unset($params['password'], $params['encrypt']);
//            }
            try {
                $row->allowField(true)->save($params);
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $this->success("修改成功！");
        }
        $this->assign("data", $row);
        return $this->fetch();
    }

    /**
     * 管理员删除
     */
    public function del()
    {
        if (false === $this->request->isPost()) {
            $this->error('未知参数');
        }
        $id = $this->request->param('id/d');
        if (empty($id)) {
            $this->error('请指定需要删除的ID！');
        }
        
        try {
            $this->modelClass->destroy($id);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success("删除成功！");

        $this->error('没有权限删除！');
    }
}
